import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * 双色球程序主入口（命令行交互）
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 1. 提示用户输入投注号码
        System.out.println("请输入您的双色球投注号码（6个红球+1个蓝球，空格分隔，如：01 05 12 18 26 33 09）：");
        String input = scanner.nextLine().trim();


        // 2. 解析用户输入
        int[] userNumbers;
        try {
            String[] parts = input.split("\\s+");
            userNumbers = new int[parts.length];
            for (int i = 0; i < parts.length; i++) {
                userNumbers[i] = Integer.parseInt(parts[i]);
            }
        } catch (NumberFormatException e) {
            System.out.println("错误：输入包含非数字字符，请重新输入！");
            scanner.close();
            return;
        }


        // 3. 校验输入合法性
        if (!LotteryChecker.validateUserInput(userNumbers)) {
            System.out.println("错误：输入格式/范围不合法！（红球：1-33不重复；蓝球：1-16）");
            scanner.close();
            return;
        }


        // 4. 拆分用户的红球和蓝球
        int[] userRed = new int[6];
        System.arraycopy(userNumbers, 0, userRed, 0, 6);
        int userBlue = userNumbers[6];


        // 5. 生成开奖号码
        int[] drawRed = generateRandomRedBalls();
        int drawBlue = generateRandomBlueBall();


        // 6. 调用中奖判断方法
        int prizeLevel = LotteryChecker.checkPrizeLevel(userRed, userBlue, drawRed, drawBlue);


        // 7. 输出结果
        System.out.print("开奖结果：");
        printNumbers(drawRed);
        System.out.println(String.format("%02d", drawBlue));


        // 输出中奖信息
        String prizeName;
        switch (prizeLevel) {
            case 1: prizeName = "一等奖"; break;
            case 2: prizeName = "二等奖"; break;
            case 3: prizeName = "三等奖"; break;
            case 4: prizeName = "四等奖"; break;
            case 5: prizeName = "五等奖"; break;
            case 6: prizeName = "六等奖"; break;
            default: prizeName = "未中奖"; break;
        }
        System.out.println("恭喜！您中了" + prizeName + "！");


        scanner.close();
    }


    /**
     * 生成随机的6个红球（1-33，不重复）
     * @return 红球数组
     */
    private static int[] generateRandomRedBalls() {
        List<Integer> redBalls = new ArrayList<>();
        for (int i = 1; i <= 33; i++) {
            redBalls.add(i);
        }
        Collections.shuffle(redBalls);
        int[] result = new int[6];
        for (int i = 0; i < 6; i++) {
            result[i] = redBalls.get(i);
        }
        return result;
    }


    /**
     * 生成随机的1个蓝球（1-16）
     * @return 蓝球号码
     */
    private static int generateRandomBlueBall() {
        return (int) (Math.random() * 16) + 1;
    }


    /**
     * 格式化输出号码（补前导零，如：03 12...）
     * @param numbers 号码数组
     */
    private static void printNumbers(int[] numbers) {
        for (int num : numbers) {
            System.out.print(String.format("%02d ", num));
        }
    }
}